package com.example.app;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * 模拟web-xml
 * <p>
 * https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/WebApplicationInitializer.html
 */
public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {

        //Spring -- applicationContext.xml
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(App.class);
        ac.refresh();

        //springmvc -- spring-mvc.xml   注册servlet
        DispatcherServlet servlet = new DispatcherServlet(ac);

        // 把一个servlet注册给tomcat
        ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);

        registration.setLoadOnStartup(1);
        //DispatcherServlet的反问路径
        registration.addMapping("*.do");
    }

}
